package dao.impl;

import java.util.List;

import entity.Goods;
import table.GoodsTable;
import abstractelement.AbstractDao;

public class GoodsDao extends AbstractDao<GoodsTable, Goods> {

	public GoodsDao(String rootFolderPath) {
		super(rootFolderPath);
	}

	@Override
	public void saveNew(Goods newGoods) {
		final GoodsTable goodsTable = deserializeFromXml();

		goodsTable.getRows().add(newGoods);

		serializeToXml(goodsTable);
	}

	@Override
	public void update(Goods entity) {
		
	}

	@Override
	public Goods get(String name) {
		final GoodsTable goodsTable = deserializeFromXml();

		for (final Goods row : goodsTable.getRows())
		{
			if (row.getName().equals(name))
			{
				return row;
			}
		}
		
		return null;
	}

	@Override
	public List<Goods> getAll() {
		final GoodsTable goodsTable = deserializeFromXml();
		
		return goodsTable.getRows();
	}

	@Override
	public void delete(String name) {
		
		final GoodsTable goodsTable = deserializeFromXml();

		for (final Goods row : goodsTable.getRows())
		{
			if(row.getName().equals(name))
			{
				goodsTable.getRows().remove(row);
				break;
			}
		}
		
		serializeToXml(goodsTable);
		
	}

	@Override
	protected Class<GoodsTable> getTableClass() {
		return GoodsTable.class;
	}

}
